#!/bin/bash

server=192.168.1.100
worker1=$server
worker2=192.168.1.102
worker3=192.168.1.103
worker4=192.168.1.104
base_folder='/home/dragan'
generation_no=$1
results_folder=`exec pwd`/results/$generation_no

job_number=0

# run dice on each worker 4 times
for i in 1 .. 4
do
for worker in $worker1 $worker2 $worker3 $worker4
do
	job_number=`expr $job_number + 1`
	
	folder=$base_folder'_'$i
	
	#thermalization phase
	run_term=$folder'/dice < ter.in > ter.out'
	
	# thermalization produces newline before the file extension 
	# so the .dat filename must be fixed because is used in the simulation phase
	rename_dat='mv CCl4?.dat CCl4.dat'
	
	# simulation phase
	run_sim=$folder'/dice < sim.in > sim.out'
	
	# copy result back to server
	# sim.out > gen_1_sim_1.out etc.
	copy_res='scp '$folder'/sim.out '$server':'$results_folder'/gen_'$generation_no'_sim_'$job_number'.out'
	
	run_dice=$run_term';'rename_dat';'$run_sim';'$copy_res';'
	
	# delete previous folder
	echo $worker' rm -rf '$folder
	ssh $worker' rm -rf '$folder
	
	# make new folder
	echo 'Make folder:'$worker:'mkdir '$folder
	ssh $worker' mkdir '$folder
	
	# copy dice folder to worker
	scp -r dice_ccl4 $worker:$folder
	
	echo '['`eval date`'] Running dice on worker:'$worker' with command:'$run_dice
	
	# run dice
	ssh $worker1 $run_dice
done
done




